<template>
  <div class="relative w-full h-full">
    <SearchCom
      :searchList="searchConfig"
      @search="handleSearch"
      @reset="handleReset"
    />
    <el-button class="mt24" type="primary" @click="handleAdd">
      <el-icon><Plus /></el-icon>
      新增活动
    </el-button>
    <el-table
      :data="tableData"
      style="width: 100%"
      class="cover-table mt12"
      height="calc(100% - 150px)"
      border
    >
      <el-table-column prop="activityName" label="活动名称" />
      <el-table-column prop="shopName" label="店铺名称" />
      <el-table-column label="活动时间范围">
        <template #default="scope">
          {{ scope.row.startTime }} 至 {{ scope.row.endTime }}
        </template>
      </el-table-column>
      <el-table-column prop="discount" label="折扣比例">
        <template #default="scope"> {{ scope.row.discount }}% </template>
      </el-table-column>
      <el-table-column prop="status" label="活动状态">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.status)">
            {{ getStatusText(scope.row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="250">
        <template #default="scope">
          <div class="flex_aic_jcc">
            <el-button
              type="text"
              size="small"
              @click="handleEdit(scope.row)"
              v-if="scope.row.status === 1"
            >
              编辑
            </el-button>
            <el-button
              type="text"
              size="small"
              @click="handleDetail(scope.row)"
            >
              详情
            </el-button>
            <el-popconfirm
              title="确定终止该活动吗？"
              @confirm="handleStop(scope.row)"
              v-if="scope.row.status === 1"
            >
              <template #reference>
                <el-button type="text" size="small">终止活动</el-button>
              </template>
            </el-popconfirm>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <div class="flex justify-end mt24 absolute bottom-0 right-0">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30, 40]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <editDia
      ref="editDiaRef"
      v-model="showEditDia"
      @success="handleSuccess"
      :data="editData"
    />
    <addDia
      ref="addDiaRef"
      v-model="showAddDia"
      @success="handleSuccess"
      :data="editData"
    />
  </div>
</template>

<script setup>
import { ref } from "vue";
import editDia from "./editDia.vue";
import addDia from "./addDia.vue";
import SearchCom from "@/components/searchCom.vue";
import { Plus } from "@element-plus/icons-vue";

const searchConfig = ref([
  {
    label: "活动名称",
    key: "activityName",
    type: "input",
  },
  {
    label: "店铺名称",
    key: "shopName",
    type: "input",
  },
  {
    label: "活动时间范围",
    key: "timeRange",
    type: "daterange",
  },
  {
    label: "活动状态",
    key: "status",
    type: "select",
    options: [
      { label: "进行中", value: 1 },
      { label: "已结束", value: 2 },
    ],
  },
]);

const addDiaRef = ref(null);
const showAddDia = ref(false);
const editDiaRef = ref(null);
const showEditDia = ref(false);
const editData = ref({});

const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);

const tableData = ref([
  {
    id: 1,
    activityName: "春季大促",
    shopName: "示例店铺",
    startTime: "2024-03-20",
    endTime: "2024-04-20",
    discount: 70,
    products: [],
    status: 1, // 1: 进行中, 2: 已结束
  },
  {
    id: 2,
    activityName: "夏季大促",
    shopName: "示例店铺2",
    startTime: "2024-06-20",
    endTime: "2024-07-20",
    discount: 80,
    status: 2,
  },
]);

const getStatusType = (status) => {
  const map = {
    1: "success",
    2: "info",
  };
  return map[status];
};

const getStatusText = (status) => {
  const map = {
    1: "进行中",
    2: "已结束",
  };
  return map[status];
};

const resetSearch = () => {
  searchForm.value = {
    activityName: "",
    shopName: "",
    timeRange: [],
  };
};

const handleSearch = () => {
  // 实现查询逻辑
};

const handleAdd = () => {
  editData.value = {};
  showAddDia.value = true;
};

const handleEdit = (row) => {
  editData.value = { ...row };
  showAddDia.value = true;
};

const handleDetail = (row) => {
  editData.value = { ...row, isView: true };
  showEditDia.value = true;
};

const handleStop = (row) => {
  // 实现终止活动逻辑
};

const handleSuccess = () => {
  handleSearch();
};

const handleSizeChange = (size) => {
  pageSize.value = size;
};

const handleCurrentChange = (page) => {
  currentPage.value = page;
};
</script>

<style lang="scss" scoped>
.toolbar {
  width: 100%;
  height: 42px;
  margin-bottom: 5px;
  background: rgba(255, 126, 40, 0.1);
}
.addBtn {
  height: 28px;
  margin-left: 10px;
  padding: 0 7px;
  border-radius: 3px;
  background: #fff;
  cursor: pointer;
}
.cover-table {
  margin-top: 20px;
}
</style>
